class util{
    //初始化对象
    constructor(){
        this.win=window.top;
        this.UA=navigator.userAgent;
        this.isPC=this.UA.indexOf('Windows NT')>-1;
        this.isAndroid=this.UA.indexOf('Android')>-1;
        this.isIos=this.UA.indexOf('Mac OS X')>-1;
        this.isIphone=this.UA.indexOf('iPhone;')>-1;
        this.isSafari=this.UA.toLowerCase().indexOf('safari')>-1;
        this.isIpad=this.UA.indexOf('iPad;')>-1;
        this.isIE7=this.UA.indexOf('MSIE 7.0;')>-1;
        this.isIE8=this.UA.indexOf('MSIE 8.0;')>-1;
        this.isIE9=this.UA.indexOf('MSIE 9.0;')>-1;
        this.isIE10=this.UA.indexOf('MSIE 10.0;')>-1;
        this.isIE11=this.UA.indexOf('Trident')>-1;
        this.isWX   = this.UA.indexOf('MicroMessenger')>-1;
        this.isWeb  = !this.isApp&&!this.isWX;
    };

    ajax(json){
        let asyncVal=typeof(json.async)=='boolean'?json.async:true;
        let conf = {
             type: json.type||"POST",
             url: json.url,
             data: JSON.stringify(json.data)||"",
             datatype:"json",
             contentType:'application/json;utf-8',
             async: asyncVal,
             success:(data)=>{
                 if(typeof(data)=='string'){
                     this.ajaxCb(JSON.parse(data),json);
                 }else{
                     this.ajaxCb(data,json);
                 }
             },
             complete:function(XMLHttpRequest){
                    
             },
             error:function(XMLHttpRequest){
                    
             }
        }
        if($.cookie('Authorization')){
            conf['headers'] = {
                Authorization:$.cookie('Authorization')
            }
        }
        $.ajax(conf);
    };

    ajaxCb(data,json){
        if(data.success){
            json.success&&json.success(data.obj);
        }else{
            if(data.code === 91 ||data.code === 92 ||data.code === 93){
                location.href="/pages/user/login.html";
                return;
            }
            popup.alert({
                title:data.msg
            })
        }
    };

    /*传入一个json数组和所需要的参数集，返回一个全新的json数组
    
    */
    getNewJson(json, arr) {
        var array = [];
        json.forEach((item, index) => {
            var obj = {};
            for (var i = 0, len = arr.length; i < len; i++) {
                for (var n in item) {
                    if (arr[i] == n) {
                        obj[arr[i]] = item[n];
                    }
                }
            }
            array.push(obj);
        })
        return array;
    }

    /*
        传入一个对象和所需要的参数集，返回一个全新的对象
    */
    getNewObj(obj, arr) {
        var object = {};
        for (var n in obj) {
            for (var i = 0, len = arr.length; i < len; i++) {
                if (n == arr[i]) {
                    object[n] = obj[n]
                }
            }
        }
        return object;
    }

    /*传入一个对象，返回该对象的值不为空的所有参数，并返回一个对象
    obj   object    传入的对象
    */
    objDislodge(obj) {
        var objData = JSON.parse(JSON.stringify(obj));
        for (var n in objData) {
            if (objData[n] == null || objData[n] == '') {
                delete objData[n]
            }
        }
        return objData;
    }

    /*传入一个json对象，返回该对象的值不为空的所有参数，并返回一个对象
    json   object    传入的对象
    */
    jsonDislodge(json) {
        var objData = JSON.parse(JSON.stringify(obj));
        objData.forEach((item, index) => {
            for (let n in item) {
                if (item[n] == null || item[n] == '') {
                    delete item[n]
                }
            }
        })
        return objData;
    }

    /*获取 storage 缓存数据
    * type  类型   local：localStorage   session：sessionStorage
    * name  缓存数据name名
    */
    getStorage(type,name){
        var type=type||'local';
        if(type=='local'){
            var result = localStorage.getItem(name)? localStorage.getItem(name):"";
        }else if(type=='session'){
            var result = sessionStorage.getItem(name)? sessionStorage.getItem(name):"";
        }
        return result;
    }

    /*设置 storage 缓存数据
    *type  类型   local：localStorage   session：sessionStorage
    *name  缓存数据name名
    *content  缓存的数据内容
    */
    setStorage(type,name,content){
        var type=type||'local';
        var data=content;
        if(typeof(data)=='object'){ data=JSON.stringify(content) };
        if(type=='local'){
            localStorage.setItem(name,data);
        }else if(type=='session'){
            sessionStorage.setItem(name,data);
        }
    }

    /*清除 storage 缓存数据
    * type  类型   local：localStorage   session：sessionStorage
    * name  缓存数据name名
    */
    removeStorage(type,name){
        var type=type||'local';
        if(type=='local'){
            localStorage.removeItem(name)
        }else if(type=='session'){
            sessionStorage.removeItem(name)
        }
    }

    //登录成功之后设置cookie
    setCookie(cookiename,value){
        var dt= new Date();
        dt.setTime(dt.getTime() + (3*24*60*60*1000));
        // $.cookie(cookiename,value,{expires:dt, path:'/' ,domain: 'morning-star.cn'});
        $.cookie(cookiename,value,{expires:dt, path:'/'});
    }

    //退出登录后移除cookie
    removeCookie(cookiename){
        // $.cookie(cookiename,"",{expires:-1, path:'/' ,domain: 'morning-star.cn'});
        $.cookie(cookiename,"",{expires:-1, path:'/' });
        sessionStorage.setItem('usermsgs',"");
    };

    //计算经纬度之间的距离
    getDisance( lat1,  lng1,  lat2,  lng2){
        var radLat1 = lat1*Math.PI / 180.0;
        var radLat2 = lat2*Math.PI / 180.0;
        var a = radLat1 - radLat2;
        var  b = lng1*Math.PI / 180.0 - lng2*Math.PI / 180.0;
        var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
        Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
        s = s *6378.137 ;// EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s.toFixed(1);
    }

    //查询url里面的参数
    urlQuery(key){
        let reg = new RegExp(`(^|&)${key}=([^&]*)&?`);
        let res = location.search.substr(1).match(reg);
        if(res !== null){return decodeURI(res[2])};
        return ''
    }
}

window.util = new util();